package cn.edu.tsinghua.weibo.util;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.mysql.jdbc.Driver;

public class DB {
	
	private static final String URL = "jdbc:mysql://127.0.0.1:3306/weibo";
	private static final String USER = "root";
	private static final String PASSWORD = "system";

	public static Connection getConnection() {
		try {
			new Driver();
			return DriverManager.getConnection(URL, USER, PASSWORD);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static void close(PreparedStatement preparedStatement, Connection connection) {
		try {
			if(preparedStatement != null)
				preparedStatement.close();
			if(connection != null)
				connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public static void close(CallableStatement callableStatement, Connection connection) {
		try {
			if(callableStatement != null)
				callableStatement.close();
			if(connection != null)
				connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public static void close(ResultSet resultSet, PreparedStatement preparedStatement, Connection connection) {
		try {
			if(resultSet != null)
				resultSet.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		close(preparedStatement, connection);
	}
	
	public static void close(ResultSet resultSet, CallableStatement callableStatement, Connection connection) {
		try {
			if(resultSet != null)
				resultSet.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		close(callableStatement, connection);
	}
	
}
